//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.core.UserConfirmCommandConfig;
	import reader.events.BlogDraftEvent;
	import reader.model.IDraftProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.BlogDraftVO;
	import reader.services.IPostBlogAble;
	import reader.services.IUpdateBlogAble;
	import reader.utils.DateRange;
	import reader.view.components.BlogWriter;
	import reader.view.components.Editor;

	public class EditorMediator extends ServiceAbleMediator implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		
		public static const NAME:String = "EditorMediator";
		
		public function get editor():Editor
		{
			return this.viewComponent as Editor;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var draftProxy:IDraftProxy;
		private var serviceProxy:IServiceProxy;
		
		private var postBlogService:IPostBlogAble;
		private var updateBlogService:IUpdateBlogAble;
		
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function EditorMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			startUp();
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.DRAFT_ALL,
									ApplicationFacade.WRITER_CLOSE,
									ApplicationFacade.WRITER_VIEW_CHANGE
									]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.DRAFT_ALL:
					var draft:Vector.<BlogDraftVO> = notification.getBody() as Vector.<BlogDraftVO>;
					editor.blogDraft = draft;
					break;
				case ApplicationFacade.WRITER_CLOSE:
					clearReference();
					break;
				case ApplicationFacade.WRITER_VIEW_CHANGE:
					if(String(notification.getBody()) == BlogWriter.VIEW_DRAFT)
					{
						draftProxy.getAllDraft();
					} 
					break;						
			}
		}
		
		override public function startUp():void
		{
			draftProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.draftProxyName) as IDraftProxy;
			serviceProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.serviceProxyName) as IServiceProxy;
			
			editor.addEventListener(FlexEvent.CREATION_COMPLETE, onCreateComplete_handler);
			editor.addEventListener(BlogDraftEvent.OPEN_BLOG_DRAFT, onEditDraft_handler);
			editor.addEventListener(BlogDraftEvent.VIEW_BLOG_DRAFT, onViewDraftDetail_handler);
			editor.addEventListener(BlogDraftEvent.DELETE_BLOG_DRAFT, onDeleteDraft_handler);
			editor.addEventListener(BlogDraftEvent.DELETE_BLOG_DRAFTS, onDeleteDrafts_handler, false, 0 , true);
		}
		
		override public function clearReference():void
		{
			draftProxy = null;
			serviceProxy = null;
			readerModelLocator = null;
			
			editor.removeEventListener(BlogDraftEvent.OPEN_BLOG_DRAFT, onEditDraft_handler);
			editor.removeEventListener(BlogDraftEvent.VIEW_BLOG_DRAFT, onViewDraftDetail_handler);
			editor.removeEventListener(BlogDraftEvent.DELETE_BLOG_DRAFT, onDeleteDraft_handler);
			editor.removeEventListener(BlogDraftEvent.DELETE_BLOG_DRAFTS, onDeleteDrafts_handler);
			
			facade.removeMediator(NAME);
		}
		
		private function refreshData():void
		{
			
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		private function onCreateComplete_handler(e:FlexEvent):void
		{
			editor.daterange = new DateRange().dateRange;
			editor.removeEventListener(FlexEvent.CREATION_COMPLETE, onCreateComplete_handler);
			draftProxy.getAllDraft();
		}
		
		private function onDeleteDrafts_handler(e:BlogDraftEvent):void
		{
			draftProxy.deleteDrafts(e.drafts);
		}
		
		private function onEditDraft_handler(e:BlogDraftEvent):void
		{
			e.stopPropagation();
			sendNotification(ApplicationFacade.EDIT_DRAFT, e.draft);
		}
		
		private function onViewDraftDetail_handler(e:BlogDraftEvent):void
		{
			e.stopPropagation();
			ApplicationFacade.getInstance().sendNotification(ApplicationFacade.AIR_EXPLORER_OPEN, e.draft.link);
		}
		
		private var deleteDraft:BlogDraftVO;
		private function onDeleteDraft_handler(e:BlogDraftEvent):void
		{
			e.stopPropagation();
			deleteDraft = e.draft;			
			var message:String = (deleteDraft.isPubed && !deleteDraft.deleteAble) ? ReaderResource.instance.resource.gridDeletePubDraftConfirm : ReaderResource.instance.resource.gridDeleteDraftConfirm;
			var cfm:UserConfirmCommandConfig = new UserConfirmCommandConfig(ReaderResource.instance.resource.gridDelete,message, editor, doDeleteDraft_handler);
			ApplicationFacade.getInstance().sendNotification(ApplicationFacade.REQUEST_USERCONFIRM, cfm);
		}
		
		private function doDeleteDraft_handler():void
		{
			var dataProvider:ArrayCollection = editor.postsGrid.dataProvider as ArrayCollection;
			dataProvider.removeItemAt(dataProvider.getItemIndex(deleteDraft));
			dataProvider.refresh();
			draftProxy.deleteDraft(deleteDraft);
			deleteDraft = null;
		}
	}
}